/* -*- C++ -*- */

/****************************************************************************
** Copyright (c) quickfixengine.org  All rights reserved.
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/

#include "Application.h"
#include "Session.h"
#include <map>

class Application : public FIX::Application
{
	void onCreate( const FIX::SessionID& sessionID )
	{
		FIX::Session * pSession = FIX::Session::lookupSession( sessionID );
		//if ( pSession ) pSession->reset();
	}

	void onLogon( const FIX::SessionID& sessionID )
		throw( FIX::RejectLogon )
	{
		FIX::Session *session = FIX::Session::lookupSession( sessionID );
		FIX::Message message = session->createMsg("A"); //checkout logon
		message.setField( FIX::FixAppVersion ( "FIXT.1.1" )); //protocol header
		message.setField( FIX::Username("USER_NAME")); //user id 
		message.setField( FIX::Password("xxxxxxx")); //password
		message.setField(1137, "5"); 
		session->send(message);

		//    //e.g. howto - create and use group
		//FIX::OrdType ordType;
		//FIX::Session *session = FIX::Session::lookupSession( sessionID );
		//FIX::Message message = session->createMsg("D");
		//message.setField(FIX::Side(FIX::Side_BUY));
		//message.setField(FIX::TransactTime(true));
		//message.setField(FIX::OrdType( FIX::OrdType_LIMIT));
		//message.setField(FIX::HandlInst('1'));
		//message.setField(FIX::Symbol( "SYMBOL_NAME" ));
		//message.setField(FIX::OrderQty( 55 ));
		//message.setField(FIX::TimeInForce( FIX::TimeInForce_DAY ));
		//message.setField(FIX::Price( 100 ));
		//FIX::NoPartyIDs noPartyIDs;
		//FIX::Group group101( 453, 448 );
		//FIX::Group group36( 453, 448 );
		//int num = 1;
		//int field = 453;
		//message.getGroup( num, group101 );
		//message.getGroup( num, group36 );
		//group101.setField(448,  "USER_NAME");
		//group101.setField(452, "101");
		//group36.setField(448, "USER_NAME");
		//group36.setField(452, "36");
		//int icount =  message.groupCount( FIX::FIELD::NoPartyIDs );
		//message.setField(453, "2");
		//message.replaceGroup(num, group101);
		//message.addGroup( group36);
		//icount =  message.groupCount( FIX::FIELD::NoPartyIDs );
		//std::string timeStamp = FIX::TransactTime(true).getString();
		//timeStamp.assign(timeStamp, 9, timeStamp.length() );
		//for ( int i = 0; i < 1; i++ )
		//{
		//				std::stringstream ss;
		//				ss << timeStamp << "-" << i ;
		//				message.setField(FIX::ClOrdID( ss.str() ));
		//				session->send(message);
		//}
	}

	void onLogout( const FIX::SessionID& sessionID )
	{
		FIX::process_sleep(20);
		FIX::Session *session = FIX::Session::lookupSession( sessionID );
		FIX::Message message = session->createMsg("5"); //checkout logout message from Factory
		std::string timeStamp = FIX::TransactTime(true).getString();
		timeStamp.assign(timeStamp, 9, timeStamp.length() );
		message.setField(58, "Logging out at:" + timeStamp);
		message.setField(1409, "0");
		session->send(message);
		FIX::process_sleep(10);
	}

	void toApp( FIX::Message& message, const FIX::SessionID& )
		throw( FIX::DoNotSend )
	{}
	void fromAdmin( const FIX::Message& message, const FIX::SessionID& sessionID)
		throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::RejectLogon ) 
	{
	}
	void toAdmin( FIX::Message& message, const FIX::SessionID& )
	{
		FIX::MsgType msgType;
		message.getHeader().getField( msgType );
		if ( strchr( "A", msgType.getValue().c_str() [ 0 ] ) != 0 )
		{
			message.setField( FIX::FixAppVersion ( "FIXT.1.1" )); //protocol header
			message.setField( FIX::Username("USER_NAME")); //user id 
			message.setField( FIX::Password("xxxxxxx")); //password
			message.setField(1137, "5"); 
		}
	}

	void fromApp( const FIX::Message& message, const FIX::SessionID& sessionID )
		throw( FIX::FieldNotFound, FIX::IncorrectDataFormat, FIX::IncorrectTagValue, FIX::UnsupportedMessageType )
	{
	}
public:
	void run(const FIX::SessionID& sessionID )
	{
		while ( true )
		{
			FIX::process_sleep(1);
		}

	}
};
